package health

import (
	"errors"
	"fmt"
	"gitcode.com/G-YT/ANN/server/global"
	"gitcode.com/G-YT/ANN/server/model/common/request"
	"gitcode.com/G-YT/ANN/server/model/health"
	healthReq "gitcode.com/G-YT/ANN/server/model/health/request"
	healthRes "gitcode.com/G-YT/ANN/server/model/health/response"
	"gitcode.com/G-YT/ANN/server/model/system"
	"gorm.io/gorm"
)

type HealthQAndAService struct {
}

// CreateHealthQAndA 创建健康问答表记录
// Author [g-qs](https://github.com/g-qs)
func (QuestionAndAnswerService *HealthQAndAService) CreateHealthQAndA(QuestionAndAnswer *health.HealthQAndA) (err error) {
	fmt.Println(QuestionAndAnswer)
	err = global.GVA_DB.Create(QuestionAndAnswer).Error
	return err
}

// DeleteHealthQAndA 删除健康问答表记录
// Author [g-qs](https://github.com/g-qs)
func (QuestionAndAnswerService *HealthQAndAService) DeleteHealthQAndA(QuestionAndAnswer health.HealthQAndA) (err error) {
	err = global.GVA_DB.Delete(&QuestionAndAnswer).Error
	return err
}

// DeleteHealthQAndAByIds 批量删除健康问答表记录
// Author [g-qs](https://github.com/g-qs)
func (QuestionAndAnswerService *HealthQAndAService) DeleteHealthQAndAByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]health.HealthQAndA{}, "id in ?", ids.Ids).Error
	return err
}

// UpdateHealthQAndA 更新健康问答表记录
// Author [g-qs](https://github.com/g-qs)
func (QuestionAndAnswerService *HealthQAndAService) UpdateHealthQAndA(QuestionAndAnswer health.HealthQAndA) (err error) {
	err = global.GVA_DB.Save(&QuestionAndAnswer).Error
	return err
}

// GetHealthQAndA 根据id获取健康问答表记录
// Author [g-qs](https://github.com/g-qs)
func (QuestionAndAnswerService *HealthQAndAService) GetHealthQAndA(id uint) (QuestionAndAnswer health.HealthQAndA, err error) {
	err = global.GVA_DB.Where("id = ?", id).Preload("User").First(&QuestionAndAnswer).Error
	return
}

// GetHealthQAndAInfoList 分页获取健康问答表记录
// Author [g-qs](https://github.com/g-qs)
func (QuestionAndAnswerService *HealthQAndAService) GetHealthQAndAInfoList(info healthReq.HealthQAndASearchReq) (list []health.HealthQAndA, total int64, err error) {

	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&health.HealthQAndA{})
	var QuestionAndAnswers []health.HealthQAndA
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	if info.Question != "" {
		db = db.Where("question LIKE ?", "%"+info.Question+"%")
	}
	var user system.SysUser
	if info.NickName != "" {
		err = global.GVA_DB.Model(&system.SysUser{}).Where("nick_name LIKE ?", "%"+info.NickName+"%").First(&user).Error
		if errors.Is(err, gorm.ErrRecordNotFound) {
			db = db.Where("author = 0")
		} else {
			db = db.Where("author = ?", user.ID)
		}
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}
	err = db.Preload("User").Find(&QuestionAndAnswers).Error
	return QuestionAndAnswers, total, err
}

// GetHealthAnswerListByQuestion 根据问题获取回答列表
// Author [g-qs](https://github.com/g-qs)
func (QuestionAndAnswerService *HealthQAndAService) GetHealthAnswerListByQuestion(info healthReq.HealthQuestionSearch, uid uint) (list []healthRes.AnswerResponse, err error) {
	fmt.Println(info.Question)
	var healthQAndAs []health.HealthQAndA
	db := global.GVA_DB.Model(&health.HealthQAndA{})
	db.Where("question LIKE ?", "%"+info.Question+"%").Find(&healthQAndAs)
	if len(healthQAndAs) > 0 {
		// 匹配到数据
		for _, v := range healthQAndAs {
			result := healthRes.AnswerResponse{
				ID:       v.ID,
				Question: v.Question,
				Answer:   v.Answer,
				Dose:     v.Dose,
				IsExist:  true,
			}
			list = append(list, result)
		}
	} else {
		// 未匹配到数据
		healthQuestion := health.HealthQuestion{
			Uid:      uid,
			Question: info.Question,
		}
		global.GVA_DB.Create(&healthQuestion)
		result := healthRes.AnswerResponse{
			ID:       9999,
			Question: info.Question,
			Answer:   "暫沒有回答",
			Dose:     "",
			IsExist:  false,
		}
		list = append(list, result)
	}
	return list, err
}
